package annotations;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class Loader {
	public static void main(String args[]) throws Exception {
		printAllAnnotations();
		invokeOnlyExecutableMethodsByExaminingMethodAnnotations();
	}

	private static void printAllAnnotations() {
		Class<SimpleClass> simpleClass = SimpleClass.class;
		Annotation[] annotations = simpleClass.getAnnotations();
		for (Annotation annotation : annotations) {
			ClassAnnotation myAnnotation = (ClassAnnotation) annotation;
			System.out.println("className: " + myAnnotation.className());
		}
		for (Method method : simpleClass.getMethods()) {
			for (Annotation methodAnnotation : method.getAnnotations()) {
				MethodAnnotation methodAnnotationTmp = (MethodAnnotation) methodAnnotation;
				System.out.println("methodShortName:" + methodAnnotationTmp.methodShortName());
			}
		}
	}

	private static void invokeOnlyExecutableMethodsByExaminingMethodAnnotations() throws Exception {
		Class<SimpleClass> simpleClass = SimpleClass.class;
		for (Method method : simpleClass.getMethods()) {
			for (Annotation methodAnnotation : method.getAnnotations()) {
				MethodAnnotation methodAnnotationTmp = (MethodAnnotation) methodAnnotation;
				if (methodAnnotationTmp.isExecutable()) {
					SimpleClass simpleClassObject = (SimpleClass) simpleClass.newInstance();
					method.invoke(simpleClassObject, (Object[]) null);
				}
			}
		}
	}

}
